package core.weapons;
import graphics.GlowParticle;
import graphics.Particle;
import graphics.SparkParticle;

import java.awt.*;

import core.Player;
import core.units.Unit;
import core.world.CollisionHandler;

public class Bullet extends Attack {
	
	double accuracy=0.04;
	int damage=0;
	
	

    public Bullet(int x1, int y1, Player m, double inDir, int dmg, int sp, double acc, int range) {
    	super(x1, y1, m);
    	speed=40;
    	accuracy=acc;
    	damage=dmg;
    	speed=sp;
    	dir=inDir-accuracy/2+Math.random()*accuracy;   	
    	x=x;
    	y=y;
    	lifetime=range/speed;
    	
    }
    public void checkHit(Unit u){
		if(hasHit==false && u.getTeam()!=team && u.bounds.contains(x, y)){
			u.takeDamage(damage);
			hasHit=true;
			//System.out.print("i");
		}			
	}
	public void nextFrame(){
		lifetime--;
		if(lifetime==0 || hasHit==true)
			onHit();
		lastPosition.setLocation(currentPosition);
		int dx = (int)(Math.cos(dir*3.1415*2)*speed);
		int dy = (int)(Math.sin(dir*3.1415*2)*speed);
		x=x+dx;
		y=y+dy;
		currentPosition.setLocation(x, y);
		if(CollisionHandler.intersects(lastPosition, currentPosition)){
			onHit();
			
		}
		int particleCount = 3+damage/30;
		for(int i=0; i<particleCount; i++){
			//main.particles.add(new Particle(x, y, 0, 0));
			Particle p = new GlowParticle((int)x-dx*i/particleCount, (int)y-dy*i/particleCount);
			
			p.setImage("glowYellow1.png");
			main.particles.add(p);
			
		}
		
		/*for(int w=0; w<main.MAXWALLS; w++){
			if(main.walls[w]!=null)
				if(main.walls[w].checkCollide(oldX, oldY, (double)x, (double)y))
					main.attacks.remove(this);
		}*/	
	}
	public void onHit(){
		for(int i=0; i<damage/5; i++)
			//main.particles.add(new Particle(x, y, 0, 0));
			main.particles.add(new SparkParticle(x, y, (int)(Math.cos(dir*3.1415*2)*speed), (int)(Math.sin(dir*3.1415*2)*speed)));
		main.attacks.remove(this);
	}

    
}